package GUI;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.cloudgarden.resource.SWTResourceManager;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class GUI_main extends org.eclipse.swt.widgets.Composite
{
	//Components of the GUI
	private Button register;
	private Label label4;
	private Label label3;
	private Text hibernatePassword;
	private Text hibernateUserName;

	{
		//Register as a resource user - SWTResourceManager will
		//handle the obtaining and disposing of resources
		SWTResourceManager.registerResourceUser(this);
	}

	public GUI_main(Composite parent, int style)
	{
		super(parent, style);
		initGUI();
	}
	
	/**
	* Initializes the GUI.
	*/
	private void initGUI() 
	{
		try 
		{
			this.setSize(457, 247);
			FormLayout thisLayout = new FormLayout();
			this.setLayout(thisLayout);
			this.setBackground(SWTResourceManager.getColor(128, 128, 128));
			// Jigloo automatic creation of label4
			{
				label4 = new Label(this, SWT.NONE);
				FormData label4LData = new FormData();
				label4LData.left =  new FormAttachment(0, 1000, 30);
				label4LData.top =  new FormAttachment(0, 1000, 75);
				label4LData.width = 150;
				label4LData.height = 14;
				label4.setLayoutData(label4LData);
				label4.setText("enter your SQL password:");
				label4.setBackground(SWTResourceManager.getColor(128, 128, 128));
			}
			// Jigloo automatic creation of label3
			{
				label3 = new Label(this, SWT.NONE);
				FormData label3LData = new FormData();
				label3LData.left =  new FormAttachment(0, 1000, 30);
				label3LData.top =  new FormAttachment(0, 1000, 42);
				label3LData.width = 154;
				label3LData.height = 14;
				label3.setLayoutData(label3LData);
				label3.setText("enter your SQL user name:");
				label3.setBackground(SWTResourceManager.getColor(128, 128, 128));
			}
			// Jigloo automatic creation of hibernatePassword
			{
				FormData hibernatePasswordLData = new FormData();
				hibernatePasswordLData.left =  new FormAttachment(0, 1000, 196);
				hibernatePasswordLData.top =  new FormAttachment(0, 1000, 75);
				hibernatePasswordLData.width = 206;
				hibernatePasswordLData.height = 14;
				hibernatePassword = new Text(this, SWT.PASSWORD);
				hibernatePassword.setLayoutData(hibernatePasswordLData);
			}
			// Jigloo automatic creation of hibernateUserName
			{
				FormData hibernateUserNameLData = new FormData();
				hibernateUserNameLData.left =  new FormAttachment(0, 1000, 196);
				hibernateUserNameLData.top =  new FormAttachment(0, 1000, 42);
				hibernateUserNameLData.width = 206;
				hibernateUserNameLData.height = 14;
				hibernateUserName = new Text(this, SWT.NONE);
				hibernateUserName.setLayoutData(hibernateUserNameLData);
			}
			// Jigloo automatic creation of register
			{
				register = new Button(this, SWT.PUSH | SWT.CENTER);
				FormData registerLData = new FormData();
				registerLData.left =  new FormAttachment(0, 1000, 180);
				registerLData.top =  new FormAttachment(0, 1000, 123);
				registerLData.width = 70;
				registerLData.height = 24;
				register.setLayoutData(registerLData);
				register.setText("register");

				register.addMouseListener(new MouseListener() 
				{
					@Override
					public void mouseUp(MouseEvent arg0) 
					{
						//the case one of the fields is empty
						if(hibernatePassword.getText().trim().equals("") || hibernateUserName.getText().trim().equals(""))
						{
							MessageBox ms = new MessageBox(getShell(), SWT.ERROR);
							ms.setMessage("Must fill in all fields");
							ms.open();
						}
						else
						{
							//creating directories in the server computer 
							new File("C:\\Program Files\\m&l_dropbox_server").mkdir();
							new File("C:\\uploadedServerFiles").mkdir();
							//update the XML of hibernate configuration
							UpdateXml ux = new UpdateXml(hibernateUserName.getText().trim(),hibernatePassword.getText().trim());
							ux.update();

							MessageBox ms = new MessageBox(getShell());
							ms.setMessage("you have istalled the server program succesfuly, please double click the server jar file to start the server");
							ms.open(); 
							System.exit(0);
						}
					}

					@Override
					public void mouseDown(MouseEvent arg0) {}

					@Override
					public void mouseDoubleClick(MouseEvent arg0) {}
				});
			}
			this.layout();
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	/**
	 * Auto-generated main method to display this 
	 * org.eclipse.swt.widgets.Composite inside a new Shell.
	 */
	public static void main(String[] args) 
	{
		Display display = Display.getDefault();
		Shell shell = new Shell(display, SWT.MIN);
		GUI_main inst = new GUI_main(shell, SWT.NULL);
		Point size = inst.getSize();
		shell.setLayout(new FillLayout());
		shell.layout();
		if(size.x == 0 && size.y == 0) 
		{
			inst.pack();
			shell.pack();
		}
		else
		{
			Rectangle shellBounds = shell.computeTrim(0, 0, size.x, size.y);
			shell.setSize(shellBounds.width, shellBounds.height);
		}
		shell.open();
		while (!shell.isDisposed()) 
		{
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}
